package com.iwinding.ladygg.ui.act;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.bluetooth.TMApi;
import com.iwinding.ladygg.modle.bean.EMSosMessageBean;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.BaseRpCallback;
import com.iwinding.ladygg.net.callback.SendSOSCallback;
import com.iwinding.ladygg.net.request.DealSOSReq;
import com.iwinding.ladygg.net.request.NoNoticeReq;
import com.iwinding.ladygg.net.request.PushSosReq;
import com.iwinding.ladygg.net.request.SendSOSReq;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.net.response.SendSOSRp;
import com.iwinding.ladygg.ui.view.SosDialogView;
import com.iwinding.ladygg.utils.AMMapHelper;
import com.iwinding.ladygg.utils.DateUtil;
import com.iwinding.ladygg.utils.SystemUtil;
import com.iwinding.ladygg.utils.ToastUtil;

/**
 * Created by hezhiyong on 2017/4/17.
 */

public class SosTestActivity extends BaseActivity implements View.OnClickListener {


    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_sos_test;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    void initView() {
        setTitleResId(R.string.icane_test);
        findViewById(R.id.lay_sos_send).setOnClickListener(this);
        findViewById(R.id.lay_sos_push).setOnClickListener(this);
        findViewById(R.id.lay_sos_details).setOnClickListener(this);
        findViewById(R.id.lay_sos_treatment).setOnClickListener(this);
        findViewById(R.id.lay_sos_cancel_send).setOnClickListener(this);
        findViewById(R.id.lay_sos_not_concerned_about).setOnClickListener(this);
        findViewById(R.id.lay_sos_send_details).setOnClickListener(this);
        findViewById(R.id.lay_sos_warning).setOnClickListener(this);
        findViewById(R.id.lay_sos_auto_trigger).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_sos_send:
                SendSOSReq sendSOSReq = new SendSOSReq();
                RequestManager.postSendSOS(mContext, sendSOSReq, new SendSOSCallback() {
                    @Override
                    public void onResult(SendSOSRp sendSOSRp) {
                        CountDownTimer cdt = new CountDownTimer(4, 5 * 60 * 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                //推送
                            }

                            @Override
                            public void onFinish() {

                            }
                        };
                        cdt.start();
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
                    }
                });
                break;
            case R.id.lay_sos_push:
                PushSosReq pushSosReq = new PushSosReq();
                RequestManager.postPushSos(mContext, pushSosReq, new SendSOSCallback() {
                    @Override
                    public void onResult(SendSOSRp sendSOSRp) {

                    }

                    @Override
                    public void onFail(String errorMsg) {
                        ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
                    }
                });
                break;
            case R.id.lay_sos_details:
                Intent detail = new Intent(mContext, SosDetailActivity.class);
                startActivity(detail);
                break;
            case R.id.lay_sos_treatment:
                DealSOSReq dealSOSReq = new DealSOSReq();
                RequestManager.postDealSOS(mContext, dealSOSReq, new BaseRpCallback<BaseRopnse>() {
                    @Override
                    public BaseRopnse parseResponse(BaseRopnse response, int id) {
                        return response;
                    }

                    @Override
                    public void onResult(BaseRopnse baseRopnse) {

                    }

                    @Override
                    public void onFail(String errorMsg) {
                        ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
                    }

                });
                break;
            case R.id.lay_sos_cancel_send:
                DealSOSReq cancelSosReq = new DealSOSReq();
                RequestManager.postCancelSOS(mContext, cancelSosReq, new BaseRpCallback<BaseRopnse>() {
                    @Override
                    public BaseRopnse parseResponse(BaseRopnse response, int id) {
                        return response;
                    }

                    @Override
                    public void onResult(BaseRopnse baseRopnse) {

                    }

                    @Override
                    public void onFail(String errorMsg) {
                        ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
                    }

                });
                break;
            case R.id.lay_sos_not_concerned_about:
                NoNoticeReq noNoticeReq = new NoNoticeReq();
                RequestManager.postNoNotice(mContext, noNoticeReq, new BaseRpCallback<BaseRopnse>() {
                    @Override
                    public BaseRopnse parseResponse(BaseRopnse response, int id) {
                        return response;
                    }

                    @Override
                    public void onResult(BaseRopnse baseRopnse) {

                    }

                    @Override
                    public void onFail(String errorMsg) {
                        ToastUtil.openBaseToast(mContext, errorMsg, Toast.LENGTH_SHORT);
                    }

                });
                break;
            case R.id.lay_sos_send_details:
                sendSOSContent(null);
                break;
            case R.id.lay_sos_warning:
                final SosDialogView sosDialogView = new SosDialogView(mContext);
                sosDialogView.show();
                sosDialogView.setOnClick(new SosDialogView.OnClick() {
                    @Override
                    public void onClickLeft() {
                        sosDialogView.hide();
                    }

                    @Override
                    public void onClickRight(Context context) {
                        Intent detail = new Intent(mContext, SosDetailActivity.class);
                        startActivity(detail);
                        sosDialogView.hide();
                    }
                });
                break;
            case R.id.lay_sos_auto_trigger://自动触发
                final SendSOSReq sosReq = new SendSOSReq();
                final AMapLocation locationInfo = AMMapHelper.getInstance().getmLocationInfo();
                if (locationInfo != null) {
                    sosReq.latitude = String.valueOf(locationInfo.getLatitude());
                    sosReq.longitude = String.valueOf(locationInfo.getLongitude());
                    sosReq.sendTime = DateUtil.getCurrentSecondTimeString();
                    TMApi.getInstance().sendSOS(locationInfo);
                } else {
                    Toast.makeText(this, "获取不到地址", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private AlertDialog alertDialog = null;

    private void createAreDialog(final String sosid) {
        if (alertDialog != null) {
            alertDialog = null;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("发送SOS急救成功");
        builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });
        builder.setPositiveButton("查看详情", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent detail = new Intent(mContext, SosDetailActivity.class);
                detail.putExtra(SosDetailActivity.SOSID, String.valueOf(sosid));
                detail.putExtra(SosDetailActivity.ISME, true);
                startActivity(detail);
            }
        });
        alertDialog = builder.create();
        alertDialog.show();
    }

    void sendSOSContent(EMSosMessageBean bean) {
        String content = "SOS紧急呼救";
        LoginRp loginRp = SystemUtil.getLoginRp(mContext);
        EMMessage message = EMMessage.createTxtSendMessage(content, loginRp.groupId);
        //如果是群聊，设置chattype，默认是单聊
        message.setChatType(EMMessage.ChatType.GroupChat);
        if (bean != null) {
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[0], Integer.parseInt(bean.type));
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[1], bean.helper);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[2], bean.helpTime);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[3], bean.userID);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[4], bean.uname);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[5], bean.mobile);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[6], bean.address);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[7], bean.sosID);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[8], bean.longtitude);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[9], bean.latitude);
            message.setAttribute(EMSosMessageBean.Attribute.VALUES[10], bean.time);
        }
//        String type = message.getStringAttribute("type", "");
        // 发送消息
        EMClient.getInstance().chatManager().sendMessage(message);
    }

    EMSosMessageBean createEMSosMessageBean(SendSOSReq sosReq, LoginRp loginRp, SendSOSRp sendSOSRp) {
        EMSosMessageBean bean = new EMSosMessageBean();
        bean.type = "0";
        bean.helper = "";
        bean.helpTime = "";//处理时间
        bean.userID = loginRp.userId;//
        bean.uname = loginRp.getName();//被救人别名
        bean.mobile = loginRp.mobile;//手机号
        bean.address = "";//地址
        bean.sosID = sendSOSRp.sosId;//急救ID
        bean.longtitude = sosReq.longitude;//地理
        bean.latitude = sosReq.latitude;//地理
        bean.time = DateUtil.getCurrentSecondTimeString();//急救时间

        return bean;
    }

}
